package com.ywgt.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ywgt.pojo.Storage;
import com.ywgt.mapper.StorageMapper;
import com.ywgt.service.StorageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ywgt.util.ResponseUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 * 文件存储表 服务实现类
 * </p>
 *
 * @author yiping
 * @since 2021-03-13
 */
@Service
public class StorageServiceImpl extends ServiceImpl<StorageMapper, Storage> implements StorageService {

    @Override
    public boolean create(String url, MultipartFile file) {
        String filename = file.getOriginalFilename();
        Storage storage=new Storage();
        storage.setName(filename);
        storage.setUrl(url);
        storage.setKey(filename.substring(filename.lastIndexOf(".")));
        try {
            storage.setSize(file.getBytes().length);
        } catch (IOException e) {
            e.printStackTrace();
        }
        storage.setType(StringUtils.getFilenameExtension(file.getOriginalFilename()));
        if(this.save(storage)){
            return true;
        }
        return false;
    }

    @Override
    public Page<Storage> getStorages(String name, String key, int page, int limit, String sort, String order) {
        PageHelper.startPage(page,limit);
        return baseMapper.selectStorages(name,key,sort,order);
    }
}
